package com.huawen.cms.app.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huawen.cms.app.dto.ArtView;
import com.huawen.cms.app.entity.Article;
import com.huawen.cms.app.entity.UserInfo;
import com.huawen.cms.app.service.ArticleService;
import com.huawen.cms.app.service.UserInfoService;
import com.huawen.cms.app.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/article")
@RestController
public class ArticleController extends BaseController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public Article getById(Long id) {
        Article article = (Article) articleService.getById(id);
        return article;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Article insert(@RequestBody Article article) {
        articleService.insert(article);
        return article;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Article update(@RequestBody Article article) {
        articleService.update(article);
        return article;
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    public List<Article> getList(@RequestBody Article article) {
        List<Article> list = articleService.getList(article);
        return list;
    }

    @RequestMapping(value = "/getPagerList", method = RequestMethod.POST)
    public PageInfo<Article> getPagerList(@RequestBody Article article) {
        Pager pager = getCurrentPager();
        PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        List<Article> list = articleService.getList(article);
        PageInfo<Article> pageInfo = new PageInfo<Article>(list);
        return pageInfo;
    }

    @RequestMapping(value = "/getArtViewList", method = RequestMethod.POST)
    public List<ArtView> getArtViewList(@RequestBody ArtView artView) {
        List<ArtView> list = articleService.getArtViewList(artView);
        return list;
    }

}
